#!/usr/bin/env bash

source "$rvm_scripts_path/functions/osx-ssl-certs"

make_sure_its_enough_ram_for_rbx()
{
  \typeset rvm_rbx_required_ram
  rvm_rbx_required_ram_mb=500

  __rvm_compiler_is_llvm ||
    __rvm_is_enough_ram ${rvm_rbx_required_ram_mb} ||
      rvm_error "For rbx (Rubinius) more than ${rvm_rbx_required_ram_mb}MB of free RAM might be required."
}

rubinius_install_detect_required_ruby()
{
  export ruby

  case "${rvm_ruby_version}" in
    (2.0.0)
      case "${rvm_ruby_patch_level}" in
        (rc1) ruby="ruby-1.9" ;;
        (*)   ruby="ruby-2.2"   ;;
      esac
      ;;

    (2*)
      case "$rvm_ruby_repo_branch" in
        (1.8.7|1.9.3) ruby="ruby-1.9|ruby-2.2" ;;
        (*)           ruby="ruby-2.2"          ;;
      esac
      ;;

    (*)
      ruby="ruby-2.3.1"
      ;;
  esac

  __rvm_ensure_has_mri_ruby "$ruby" || {
      rvm_log "No $ruby found, cannot build rbx."
      return 1
  }

  ruby="$(__rvm_mri_ruby "$ruby")"
}

rubinius_install_ensure_gem()
{
  __rvm_which "$1" >/dev/null &&
  [[ -x "$rvm_wrappers_path/$ruby/$1" ]] ||
  __rvm_log_command "install.${2:-$1}" \
    "$ruby - #install gem ${2:-$1}" \
    gem_install ${2:-$1} &&
  __rvm_which "$1" >/dev/null &&
  [[ -x "$rvm_wrappers_path/$ruby/$1" ]] ||
  return $?
}

rubinius_install_regenerate_binstubs()
{
  gem_install executable-hooks &&
  gem regenerate_binstubs ||
  return $?
}

rubinius_install_ensure_wrappers()
{
  [[ -x "$rvm_wrappers_path/$ruby/ruby" && -x "$rvm_wrappers_path/$ruby/gem" ]] ||
    __rvm_log_command "gemset.wrappers.rubinius" "$ruby - #generating wrappers" \
    run_gem_wrappers regenerate ||
    return $?
  rubinius_install_ensure_gem rake || return $?
  rubinius_install_ensure_gem bundle bundler || return $?
  __rvm_log_command "regenerate_binstubs" \
    "$ruby - #regenerating binstubs" \
    rubinius_install_regenerate_binstubs ||
    return $?
}

rubinius_install_setup_ruby()
{
  if [[ ${rvm_ignore_gemsets_flag:-0} -eq 0 ]]
  then ruby="${ruby}@rubinius"
  fi
  __rvm_with "$ruby" rubinius_install_ensure_wrappers
  __rvm_osx_ssl_certs_ensure_for_ruby "$rvm_wrappers_path/$ruby/ruby"
}

rubinius_install()
{
  rubinius_install_detect_required_ruby || return $?

  make_sure_its_enough_ram_for_rbx

  \typeset rvm_configure_args
  \typeset -a rvm_ruby_configure
  unset CFLAGS LDFLAGS ARCHFLAGS # Important.
  unset GEM_HOME GEM_PATH MY_RUBY_HOME IRBRC RBXOPT
  export PATH
  rvm_ruby_make=""

  __rvm_setup_compile_environment "${rvm_ruby_string}" || return $?
  __rvm_remove_rvm_from_path
  __rvm_conditionally_add_bin_path

  rubinius_install_setup_ruby || return $?

  __rvm_fetch_ruby || return $?

  __rvm_cd "${rvm_src_path}/$rvm_ruby_string"

  chmod +x ./configure

  __rvm_apply_patches ||
  {
    result=$?
    rvm_error "There has been an error while trying to apply patches to rubinius.  \nHalting the installation."
    return $result
  }
  rvm_configure_flags=( --prefix="$rvm_ruby_home"  "${rvm_configure_flags[@]}" )

  __rvm_db "${rvm_ruby_interpreter}_configure_flags" "db_configure_flags"
  if [[ -n "${ZSH_VERSION:-}" ]]
  then rvm_configure_flags=( ${=db_configure_flags} "${rvm_configure_flags[@]}" )
  else rvm_configure_flags=( ${db_configure_flags}  "${rvm_configure_flags[@]}" )
  fi

  # Explicitly disabled
  (( ${rvm_llvm_flag:=1} )) || rvm_configure_flags+=( --disable-llvm )

  if
    [[ -f "Gemfile" ]]
  then
    __rvm_log_command "bundle" "$ruby - #bundle install" "$rvm_wrappers_path/$ruby/bundle" install ||
      return $?
  fi
  if
    [[ -f "$HOME/.gem/credentials" && -w "$HOME/.gem/credentials" ]]
  then
    chmod 600 "$HOME/.gem/credentials"
  fi
  __rvm_log_command "configure" "$rvm_ruby_string - #configuring" \
    env "${rvm_configure_env[@]}" $rvm_wrappers_path/$ruby/ruby ./configure "${rvm_configure_flags[@]}" ||
    return $?

  if [[ -n "${rvm_configure_args:-}" ]]
  then rvm_ruby_make="CONFIGURE_ARGS=${rvm_configure_args## } "
  fi

  __setup_lang_fallback
  __rvm_log_command rake "$rvm_ruby_string - #compiling" \
    "${rvm_ruby_make:-}$rvm_wrappers_path/$ruby/rake" install --trace ||
    return $?
  [[ -d "$rvm_ruby_home" && -f "$rvm_ruby_home/bin/rbx" ]] ||
  {
    result=$?
    rvm_error "Rubinius reported it was installed successfully, but RVM could not find proper installation,
please check why '$rvm_ruby_home/bin/rbx' was not created,
maybe here '$rvm_log_path/$rvm_ruby_string/rake.log',
also make sure to report an error here 'https://github.com/rubinius/rubinius/issues'
about not returning non zero status in case of errors."
    return $result
  }
  unset ruby

  # Symlink rubinius wrapper if not available
  [[ -f "$rvm_ruby_home/bin/ruby" ]] ||
    ln -fs "$rvm_ruby_home/bin/rbx" "$rvm_ruby_home/bin/ruby"

  # remove the rdoc link, the gem is not builtin
  if [[ -f "$rvm_ruby_home/bin/rdoc" ]]
  then rm -f "$rvm_ruby_home/bin/rdoc"
  fi

  case ${rvm_ruby_string} in
    rbx-head*|rubinius-head*|rbx-2.*|rubinius-2.*)
      binaries=()
      ;;
    *) # older rbx had issues
      # Install IRB Wrapper on Rubinius.
      file_name="$rvm_ruby_home/bin/irb"
      \command \rm -f "$file_name"
      printf "%b" '#!/usr/bin/env bash\n' > "$file_name"
      printf "%b" "exec '$rvm_ruby_home/bin/rbx' 'irb' \"\$@\"\n" >> "$file_name"
      [[ -x "$file_name" ]] || chmod +x "$file_name"

      # Install Gem Wrapper on Rubinius.
      file_name="$rvm_ruby_home/bin/gem"
      __rvm_cp -f "$rvm_ruby_home/lib/bin/gem.rb" "$file_name"
      __rvm_inject_ruby_shebang "$file_name"
      [[ -x "$file_name" ]] || chmod +x "$file_name"
      unset file_name
      binaries=(erb ri)
      ;;
  esac

  __rvm_initial_gemsets_create "$rvm_ruby_home/bin/ruby" &&
  __rvm_post_install &&
  __rvm_fetch_ruby_cleanup
}
